package comman.duke 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author jerry.d
	 */
	public class LoadItem 
	{
		public var groupName:String;
		public var assetName:String;
		public var group:Array;
		public var tryTimes:int = 3;
		private var request:URLRequest;
		public var contentType:uint;
		
		private var loader:Loader;
		private var urlLoader:URLLoader;
		public function LoadItem() 
		{
			super();
			this.loader = new Loader();
			this.urlLoader = new URLLoader();
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete);
			this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			this.urlLoader.addEventListener(Event.COMPLETE, this.onComplete);
			this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
		}
		
		private function onProgress(evt:ProgressEvent):void{
			
		}
		
		private function onComplete(evt:Event):void{
			ResourceLoader.Instance.assetLoadComplete(this);
			if ( this.groupName == null){
				ResourceLoader.Instance.loadComplete(this);
			}else{
				this.loadGroup();
			}
			
		}
		
		private function onError(evt:IOErrorEvent):void{
			GameUtils.fatal(evt.text);
			if ( this.tryTimes > 0){
				this.loadAsset(this.assetName);
				this.tryTimes--;
			}
		}
		
		public function reset():void{
			this.assetName = null;
			this.groupName = null;
			this.tryTimes = 3;
		}
		
		public function loadGroup(groupName:String = null, group:Array = null):void{
			if ( groupName != null && group != null){
				this.groupName = groupName;
				this.group = group;
			}
			
			var obj:*;
			var result:*;
			var got:Boolean = false;
			while ( this.group.length != 0){
				obj = this.group.pop();
				result = ResourceLoader.Instance.getAsset(obj.url);
				if ( result == null){
					got = true;
					this.loadAsset(obj.url, obj.type);
					break;
				}
			}
			if ( this.group.length == 0 && !got){
				ResourceLoader.Instance.loadComplete(this);
			}
		}
		
		private function loadAsset(url:String,type:uint=0):void{
			this.request = new URLRequest(url);
			this.contentType = type;
			if ( this.contentType == ResourceLoader.TYPE_IMAGE){
				this.loader.load(request);
			}else{
				this.urlLoader.dataFormat = this.contentType == ResourceLoader.TYPE_BINATY ? URLLoaderDataFormat.BINARY : URLLoaderDataFormat.TEXT;
				this.urlLoader.load(request);
			}
			this.assetName = url;
		}
		
		public function get content():*{
			if ( this.contentType == ResourceLoader.TYPE_IMAGE){
				return this.loader.content;
			}else if( this.contentType == ResourceLoader.TYPE_TEXT){
				return this.urlLoader.data;
			}else if ( this.contentType == ResourceLoader.TYPE_BINATY){
				return this.urlLoader.data;
			}
		}
	}

}